package top.kaoshanji.p1ch02a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;

/**
 * author kaoshanji
 * ^_^
 * create time 2024/12/23 15:24
 */
public class FieldUsage {

    private static final Logger logger = LogManager.getLogger(FieldUsage.class);

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

        FieldUsage fu = new FieldUsage();

        fu.useField();

    }

    public void useField() throws NoSuchFieldException, IllegalAccessException {
        logger.info("...代码清单 p1ch02a-04：使用反射API获取和使用静态域和实例域(2.2.2节)...");

        // 静态字段
        Field fieldCount = FieldContainer.class.getDeclaredField("count");
        fieldCount.set(null, 2024);

        // 实例字段
        Field fieldName = FieldContainer.class.getDeclaredField("name");
        FieldContainer fieldContainer = new FieldContainer();
        fieldName.set(fieldContainer, "Musk");

        System.out.println(FieldContainer.count);
        System.out.println(fieldContainer.name);

    }


}
